var modifyOrAddFlag;
var socketLogParam = null;
$(document).ready(
		function() {
			var urlParamObj = getParamobjFromUrl();
			if(urlParamObj.interfaceName){
				$('#txtIfNameSocket').val(decodeURI(urlParamObj.interfaceName));
				$('#wsdlTabs').tabs('select', "SOCKET服务日志列表");
			}
			
			// 初始化查询日期
			initailDateSocket();
			// 请求结果下拉框
			$('#comboxReqResultSocket').combobox(
					{
						url : BASE_URL_DATA_DICTIONARY_VALUE_SEARCH
								+ '/findByTypeCode?typeCode='
								+ DDTC_REQUEST_RESULT + '&v=' + Math.random(),
						valueField : 'valueCode',
						textField : 'valueName',
						editable : false,
						method : 'get',
						loadFilter : function(data) {
							var allCheck = {
								'valueCode' : '',
								'valueName' : '全部'
							};
							data.unshift(allCheck);
							return data;
						},
						onLoadSuccess:function(){
							
					    }
					});

			if(urlParamObj.interfaceName){
				$('#comboxReqResultSocket').combobox('setValue', '0');
				initailDateSocketForWord();
			}
			
			
			$("#datagridSocket").datagrid(
					{
						url : BASE_URL_SOCKET_LOG_SEARCH
								+ '/findByExample?v='
								+ Math.random(),
						method : 'get',
						autoRowHeight : true,
						singleSelect : true,
						striped : true,
						nowrap : false,
						pageSize : 20,
						pageList : [ 10, 20, 50, 100 ],
						columns : [[{field:'fieldName',title:'领域',width:20},
									{field:'systemName',title:'系统',width:20},{
							field : 'interfaceName',
							title : '接口名称',
							width : 30,
							align : 'left'
						}, {
							field : 'sourceAddress',
							title : '源主机IP',
							width : 30,
							align : 'center'
						}, {
							field : 'targetAddress',
							title : '目标主机IP',
							width : 30,
							align : 'center'
						}, {
							field : 'fileName',
							title : '报文头',
							width : 20,
							align : 'left'
						}, {
							field : 'fileSize',
							title : '大小(byte)',
							width : 18,
							align : 'center'
						}, {
							field : 'executeTaskNode',
							title : '执行任务节点',
							width : 20,
							align : 'center'
						}, {
							field : 'transportDate',
							title : '传输时间',
							width : 30,
							align : 'center'
						}, {
							field : 'transportResult',
							title : '传输结果',
							width : 15, formatter: function(value,row,index){
								if(value == "0"){
									return "失败";
								}else if(value == "1"){
									return "成功";
								}
							}
						}, {
							field : 'transportResultDes',
							title : '传输结果描述',
							width : 30,
							align : 'center'
						}]],
						pagination : true,
						onSelect : function(rowIndex, rowData) {
						},
						onUnselect : function() {
						},
						loadFilter : function(data) {
							// alert("inData:"
							// + JSON.stringify(data));
							return {
								total : data.totalElements,
								rows : data.content
							};
						},
						onBeforeLoad : function(param) {
							param.page -= 1;
							param.size = param.rows;
							delete param.rows;

							param.sort = 'createTime,desc';

							var interfaceName = $('#txtIfNameSocket').val();
							if (interfaceName != "") {
								param.interfaceName = interfaceName;
							}
							var transportResult = $('#comboxReqResultSocket').combobox('getValue');
							if (transportResult != "") {
								param.transportResult = transportResult;
							}
							/*var beginOfCreateTime = $("#txtStartDateTimeFtp").datebox(
									'getValue');
							if (beginOfCreateTime != "") {
								param.beginOfCreateTime = beginOfCreateTime;
							}
							var endOfCreateTime = $("#txtEndDateTimeFtp").datebox(
									'getValue');
							if (endOfCreateTime != "") {
								param.endOfCreateTime = endOfCreateTime;
							}*/
							var createTime = $("#txtStartDateTimeSocket").datebox(
							'getValue');
							if (createTime != "") {
								param.createTime = createTime;
							}
							var endTime = $("#txtEndDateTimeSocket").datebox(
									'getValue');
							if (endTime != "") {
								param.endTime = endTime;
							}
							
							socketLogParam = param;
						}
					});

			$("#datagridSocket").datagrid('getPager').pagination({
				displayMsg : '当前显示从 [{from}] 到 [{to}] 共[{total}]条记录'
			});
			$(".panel-tool-close, #instance_cancel").click(function(e) {
				$("body").css("overflow-x", "inherit")
			});

		});

function socketLogExport() {
	var startDateTime = $("#txtStartDateTimeSocket").datebox('getValue');
	if (startDateTime == null || startDateTime == "") {
		$.messager.alert('警告', "<p>开始时间不能为空!</p>", "warning");
		return;
	}
	var endDateTime = $("#txtEndDateTimeSocket").datebox('getValue');
	if (endDateTime == null || endDateTime == "") {
		$.messager.alert('警告', "<p>结束时间不能为空!</p>", "warning");
		return;
	}
	// 开始年月
	var sDate = new Date(startDateTime);
	var years = sDate.getFullYear();
	var months = sDate.getMonth();
	var days = sDate.getDate();
	// 结束年月
	var eDate = new Date(endDateTime);
	var yeare = eDate.getFullYear();
	var monthe = eDate.getMonth();
	var daye = eDate.getDate();
	var start=startDateTime.substring(0,7);
	var end=endDateTime.substring(0,7);
	if (start != end) {
		$.messager.alert('警告', '<p>只能导出同年同月内的数据，不能跨年跨月导出!</p>', "warning");
		return;
	}
	if (daye - days > 7) {
		$.messager.alert('警告','<p>只能导出传输时间的时间区间最多为7天的数据！</p>', 'warning');
		return;
	}
	$('#datagridSocket').datagrid('reload');
	var param = JSON.stringify(socketLogParam);
	location.href= BASE_URL_EXPORTDATA_FILE + '/socketLogExport?param='+param;
}


function initailDateSocket() {

	var curr_time = new Date();
	var year = curr_time.getFullYear();
	var month = curr_time.getMonth() + 1;
	var day = curr_time.getDate();
	var hours = curr_time.getHours();
	var minutes = curr_time.getMinutes();
	var seconds = curr_time.getSeconds();
	if (month < 10) {
		month = '0' + month;
	}
	if (day < 10) {
		day = '0' + day;
	}
	if (hours < 10) {
		hours = '0' + hours;
	}
	if (minutes < 10) {
		minutes = '0' + minutes;
	}
	if (seconds < 10) {
		seconds = '0' + seconds;
	}
	var start = year + '-' + month + '-' + day + ' 00:00:00';
	var end = year + '-' + month + '-' + day + ' 23:59:59';

	$("#txtStartDateTimeSocket").datetimebox("setValue", start);
	$("#txtEndDateTimeSocket").datetimebox("setValue", end);
}

function initailDateSocketForWord() {

	var curr_time = new Date();
	var year = curr_time.getFullYear();
	var month = curr_time.getMonth() + 1;
	var day = curr_time.getDate();
	var hours = curr_time.getHours();
	var minutes = curr_time.getMinutes();
	var seconds = curr_time.getSeconds();
	if (month < 10) {
		month = '0' + month;
	}
	var dayStart = day -3;
	if (dayStart < 10) {
		dayStart = '0' + dayStart;
	}
	
	if (day < 10) {
		day = '0' + day;
	}
	if (hours < 10) {
		hours = '0' + hours;
	}
	if (minutes < 10) {
		minutes = '0' + minutes;
	}
	if (seconds < 10) {
		seconds = '0' + seconds;
	}
	var start = year + '-' + month + '-' + dayStart + ' 00:00:00';
	var end = year + '-' + month + '-' + day + ' 23:59:59';

	$("#txtStartDateTimeSocket").datetimebox("setValue", start);
	$("#txtEndDateTimeSocket").datetimebox("setValue", end);
}

// 查询
function searchSocket() {
	var startDateTime = $("#txtStartDateTimeSocket").datebox('getValue');
	if (startDateTime == null || startDateTime == "") {
		$.messager.alert('警告', "<p>开始时间不能为空!</p>", "warning");
		return;
	}
	var endDateTime = $("#txtEndDateTimeSocket").datebox('getValue');
	if (endDateTime == null || endDateTime == "") {
		$.messager.alert('警告', "<p>结束时间不能为空!</p>", "warning");
		return;
	}
	// 开始年月
	var sDate = new Date(startDateTime);
	var years = sDate.getFullYear();
	var months = sDate.getMonth();
	var days = sDate.getDate();
	// 结束年月
	var eDate = new Date(endDateTime);
	var yeare = eDate.getFullYear();
	var monthe = eDate.getMonth();
	var daye = eDate.getDate();
	var start=startDateTime.substring(0,7);
	var end=endDateTime.substring(0,7);
	if (start != end) {
		$.messager.alert('警告', '<p>只能查询同年同月内的数据，不能跨年跨月查询!</p>', "warning");
		return;
	}
	if (daye - days > 7) {
		$.messager.alert('警告','<p>传输时间的查询区间最多只能查7天的！</p>', 'warning');
		return;
	}
	$('#datagridSocket').datagrid('reload');
}
function refresh1() {
	$('#datagridSocket').datagrid('reload');
}

var loading_point = {
	timer : null,
	len : 3,
	count : 0
};
function loadingSocket() {
	if (loading_point.count < loading_point.len) {
		loading_point.count++;
		$("#progressbar_point").text($("#progressbar_point").text() + ".");
	} else {
		loading_point.count = 0;
		$("#progressbar_point").text("");
	}
}
